package com.rabbiter.oes.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbiter.oes.entity.Admin;
import com.rabbiter.oes.entity.Student;
import com.rabbiter.oes.entity.Teacher;
import com.rabbiter.oes.mapper.TeacherMapper;
import com.rabbiter.oes.service.TeacherService;
import com.rabbiter.oes.utils.PasswordEncryptionExample;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public IPage<Teacher> findAll(Page<Teacher> page, String name, String sex, String tel, String cardId) {
        name = ("@".equals(name) ? "" : name);
        sex = ("@".equals(sex) ? "" : sex);
        tel = ("@".equals(tel) ? "" : tel);
        cardId = ("@".equals(cardId) ? "" : cardId);
        IPage<Teacher> teachers = baseMapper.findAllByTerms(page, name, sex, tel, cardId);
        return teachers;
    }

    @Override
    public List<Teacher> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public Long findCountByAcc(String acc) {
        return baseMapper.findCountByAcc(acc);
    }

    @Override
    public Teacher findById(Integer id) {
        return baseMapper.findById(id);
    }

    @Override
    public int deleteById(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int update(Teacher teacher) {
        // 修改教师信息时不对密码进行修改操作
        Teacher tea = baseMapper.findById(teacher.getId());
        Assert.isTrue(ObjectUtil.isNotEmpty(tea), "未找到将要修改的教师信息");
        teacher.setPwd(tea.getPwd());
        return baseMapper.updateById(teacher);
    }

    @Override
    public int updateByPassword(Teacher teacher) {
        teacher.setPwd(PasswordEncryptionExample.encrypted(teacher.getPwd()));
        return baseMapper.updateById(teacher);
    }

    @Override
    public int add(Teacher teacher) {
        teacher.setRole("1");
        // 对教师进行添加操作前对密码加密
        teacher.setPwd(PasswordEncryptionExample.encrypted(teacher.getPwd()));
        return baseMapper.insert(teacher);
    }

    @Override
    public void resetPsw(Integer id, String newPsw, String oldPsw) {
        Assert.isTrue(ObjectUtil.isNotEmpty(id), "未获取到id");
        Assert.isTrue(ObjectUtil.isNotEmpty(newPsw), "新密码未获取到");
        Assert.isTrue(ObjectUtil.isNotEmpty(oldPsw), "确认密码未获取到");
        Teacher teacher = baseMapper.selectById(id);
        Assert.isTrue(ObjectUtil.isNotEmpty(teacher), "教师信息未获取到");
        Assert.isTrue(PasswordEncryptionExample.checkPassword(oldPsw, teacher.getPwd()), "原密码错误");

        teacher.setPwd(PasswordEncryptionExample.encrypted(newPsw));
        baseMapper.updateById(teacher);
    }

}
